package com.zbkj.service.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zbkj.common.enums.OrderStatusEnum;
import com.zbkj.common.model.order.OrderInfo;
import com.zbkj.common.model.user.User;
import com.zbkj.common.oss.OssClientUtil;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.order.AdminOrderQueryRequest;
import com.zbkj.common.utils.CommonUtil;
import com.zbkj.common.vo.order.OrderConditionVo;
import com.zbkj.service.dao.order.OrderInfoMapper;
import com.zbkj.service.service.order.OrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-15
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OssClientUtil ossClientUtil;

    @Override
    public PageInfo<OrderInfo> getAdminList(AdminOrderQueryRequest paramRequest) {
        Page<OrderInfo> pageObj = PageHelper.startPage(paramRequest.getPage(),paramRequest.getLimit());
        List<OrderInfo> list = orderInfoMapper.getAdminList(paramRequest);
        return CommonPage.copyPageInfo(pageObj, list);
    }

    @Override
    public OrderInfo getOrderInfoByOrderNo(String orderNo) {
        if(StringUtils.isBlank(orderNo)) {
            return null;
        }
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getDeleteFlag,0);
        if(StringUtils.isNotBlank(orderNo)) {
            wrapper.eq(OrderInfo::getOrderNo, orderNo);
        }
        return this.getOne(wrapper,false);
    }



    @Override
    public OrderInfo getOrderInfo(Long orderId, String orderNo) {
        if(Objects.isNull(orderId) && StringUtils.isBlank(orderNo)) {
            return null;
        }
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getDeleteFlag,0);
        if(Objects.nonNull(orderId)) {
            wrapper.eq(OrderInfo::getId, orderId);
        }
        if(StringUtils.isNotBlank(orderNo)) {
            wrapper.eq(OrderInfo::getOrderNo, orderNo);
        }
        return this.getOne(wrapper,false);
    }



    @Override
    public PageInfo<OrderInfo> getOrderList(Long userId, PageParamRequest paramRequest) {
        List<OrderInfo> list = Lists.newArrayList();
        if(Objects.isNull(userId)) {
            return new PageInfo<OrderInfo>(list);
        }
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getDeleteFlag,0)
                .eq(OrderInfo::getSourceType,0)
                .eq(OrderInfo::getUserId,userId)
                .orderByDesc(OrderInfo::getId);
        Page<OrderInfo> pageObj = PageHelper.startPage(paramRequest.getPage(), paramRequest.getLimit());
        list = this.list(wrapper);
        return CommonPage.copyPageInfo(pageObj, list);
    }

    @Override
    public List<String> payVouchesStrToShowList(String payVouches) {
        List<String> resultList = Lists.newArrayList();
        if(StringUtils.isBlank(payVouches)) {
            return resultList;
        }
        String[] strs = payVouches.split(",");
        for(String str: strs) {
            if(StringUtils.isNotBlank(str)) {
                resultList.add(ossClientUtil.addDomain(str));
            }
        }
        return resultList;
    }

    @Override
    public String payVouchesToSaveStrList(List<String> payVouchesList) {
        if(CollectionUtils.isEmpty(payVouchesList)) {
            return null;
        }
        StringJoiner strJoiner = new StringJoiner(",");
        for(String str : payVouchesList) {
            if(StringUtils.isNotBlank(str)) {
                strJoiner.add(ossClientUtil.clearDomain(str));
            }
        }
        String resStr = strJoiner.toString();
        return StringUtils.isBlank(resStr) ? null : resStr;
    }

    @Override
    public boolean updateOrder(OrderInfo orderInfo, OrderConditionVo conditionVo) {
        if(Objects.isNull(orderInfo) || Objects.isNull(orderInfo.getId())) {
            return false;
        }
        LambdaUpdateWrapper<OrderInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrderInfo::getDeleteFlag, 0)
                .eq(OrderInfo::getId, orderInfo.getId());
        if(Objects.nonNull(conditionVo)) {
            if(CollectionUtils.isNotEmpty(conditionVo.getOrderStatusList())) {
                updateWrapper.in(OrderInfo::getOrderStatus, conditionVo.getOrderStatusList());
            }
        }
        return this.update(orderInfo, updateWrapper);
    }

    @Override
    public OrderInfo saveOrderPojoBuild(User user, int sourceType) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderNo(CommonUtil.getUuid());
        orderInfo.setSourceType(sourceType);
        orderInfo.setUserId(user.getId());
        orderInfo.setUserPhone(user.getPhone());
        orderInfo.setCompanyName(user.getCompanyName());
        orderInfo.setPayType(1);
        orderInfo.setOrderStatus(OrderStatusEnum.ORDER_STATUS1.getStatus());
        return orderInfo;
    }

    @Override
    public int getCountByTfremark(String code) {
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getDeleteFlag,0)
                .in(OrderInfo::getOrderStatus,Lists.newArrayList(OrderStatusEnum.tfCodeRepeatCheckStatus()));
        return this.count(wrapper);
    }

    @Override
    public PageInfo<OrderInfo> getOrderList(Long userId, int page, int limit, Integer invoiceFlag) {
        List<OrderInfo> list = Lists.newArrayList();
        if(Objects.isNull(userId)) {
            return new PageInfo<OrderInfo>(list);
        }
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getDeleteFlag,0)
                .eq(OrderInfo::getSourceType,0)
                .eq(OrderInfo::getUserId,userId)
                .orderByDesc(OrderInfo::getId);
        if(invoiceFlag == 0){
            wrapper.and(wq ->wq.eq(OrderInfo::getInvoiceFlag, invoiceFlag).or().isNull(OrderInfo::getInvoiceFlag));
        }else{
            wrapper.eq(OrderInfo::getInvoiceFlag, invoiceFlag);
        }
        Page<OrderInfo> pageObj = PageHelper.startPage(page, limit);
        list = this.list(wrapper);
        return CommonPage.copyPageInfo(pageObj, list);
    }
}
